package com.example;

import java.util.concurrent.CountDownLatch;

public class ThreadWaitingWithCountDownLatch {

    public static void main(String[] args) {
        CountDownLatch latch = new CountDownLatch(1);

        Thread mainThread = Thread.currentThread();
        Thread testThread = new Thread(()->{
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("-------------");
            System.out.println("主线程状态：" + mainThread.getState());
            System.out.println("执行latch.countDown()...");
            latch.countDown();
        }, "test-thread");
        testThread.start();

        try {
            System.out.println("即将执行latch.await()，主线程即将进入WAITING状态");
            latch.await();
            System.out.println("主线程继续运行");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
